package pl.multimedialne.carculator.gui;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

import pl.multimedialne.carculator.R;
import pl.multimedialne.carculator.gui.dataContainers.JournalsDataContainer;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

public class JournalFragment extends Fragment {
	
	JournalsDataContainer container;
	EditText fuelAmount;
	Spinner fuelTypeSpinner;
	EditText currentOdo;
	
	TextView[] dates;
	TextView[] fuelAmounts;
	TextView[] fuelTypes;
	TextView[] currentOdos;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_journal,
				container, false);
		return rootView;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) { 
	    super.onActivityCreated(savedInstanceState);
	    

    	fuelAmount = (EditText) getView().findViewById(R.id.editTextFuelAmount);
    	fuelTypeSpinner = (Spinner) getView().findViewById(R.id.spinnerFuelType);
    	currentOdo = (EditText) getView().findViewById(R.id.editTextCurrentODO);

	    LoadContainer();
	    
		ArrayAdapter<CharSequence> fuelTypeAdapter = ArrayAdapter.createFromResource(
				getActivity(), R.array.fuel_types,
				android.R.layout.simple_spinner_item);
		fuelTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		fuelTypeSpinner.setAdapter(fuelTypeAdapter);

		Button buttonNewRefuel = (Button) getView().findViewById(R.id.buttonNewRefuel);

		buttonNewRefuel.setOnClickListener(new View.OnClickListener() { 
	    public void onClick(View view) { 
	    	LinearLayout newRefuelLayout = (LinearLayout) getView().findViewById(R.id.newRefuelLayout);
	    	newRefuelLayout.setVisibility(View.VISIBLE);
	       }
	    });
		
		Button buttonAddRefuel = (Button) getView().findViewById(R.id.buttonAddRefuel);

		buttonAddRefuel.setOnClickListener(new View.OnClickListener() { 
	    public void onClick(View view) { 
	    	if (!fuelAmount.getText().toString().isEmpty() && !currentOdo.getText().toString().isEmpty()){
	    	container.AddRefuel(new Date(), Double.parseDouble(fuelAmount.getText().toString()), 
	    			fuelTypeSpinner.getSelectedItemPosition(), Double.parseDouble(currentOdo.getText().toString()));
	    	fuelAmount.setText("");
	    	currentOdo.setText("");
	    	updateView();
	    	}
	    	
	    	LinearLayout newRefuelLayout = (LinearLayout) getView().findViewById(R.id.newRefuelLayout);
	    	newRefuelLayout.setVisibility(View.GONE);
	       }
	    });
		
		Button buttonCancelNewRefuel = (Button) getView().findViewById(R.id.buttonCancelNewRefuel);

		buttonCancelNewRefuel.setOnClickListener(new View.OnClickListener() { 
	    public void onClick(View view) { 
	    	LinearLayout newRefuelLayout = (LinearLayout) getView().findViewById(R.id.newRefuelLayout);
	    	newRefuelLayout.setVisibility(View.GONE);
	       }
	    });
	}

	private void LoadContainer()
	{
		dates = new TextView[8];
		fuelAmounts = new TextView[8];
		fuelTypes = new TextView[8];
		currentOdos = new TextView[8];
		
		dates[0] = (TextView) getView().findViewById(R.id.textViewTableRow00);
		dates[1] = (TextView) getView().findViewById(R.id.textViewTableRow10);
		dates[2] = (TextView) getView().findViewById(R.id.textViewTableRow20);
		dates[3] = (TextView) getView().findViewById(R.id.textViewTableRow30);
		dates[4] = (TextView) getView().findViewById(R.id.textViewTableRow40);
		dates[5] = (TextView) getView().findViewById(R.id.textViewTableRow50);
		dates[6] = (TextView) getView().findViewById(R.id.textViewTableRow60);
		dates[7] = (TextView) getView().findViewById(R.id.textViewTableRow70);
		
		fuelAmounts[0] = (TextView) getView().findViewById(R.id.textViewTableRow01);
		fuelAmounts[1] = (TextView) getView().findViewById(R.id.textViewTableRow11);
		fuelAmounts[2] = (TextView) getView().findViewById(R.id.textViewTableRow21);
		fuelAmounts[3] = (TextView) getView().findViewById(R.id.textViewTableRow31);
		fuelAmounts[4] = (TextView) getView().findViewById(R.id.textViewTableRow41);
		fuelAmounts[5] = (TextView) getView().findViewById(R.id.textViewTableRow51);
		fuelAmounts[6] = (TextView) getView().findViewById(R.id.textViewTableRow61);
		fuelAmounts[7] = (TextView) getView().findViewById(R.id.textViewTableRow71);
		
		fuelTypes[0] = (TextView) getView().findViewById(R.id.textViewTableRow02);
		fuelTypes[1] = (TextView) getView().findViewById(R.id.textViewTableRow12);
		fuelTypes[2] = (TextView) getView().findViewById(R.id.textViewTableRow22);
		fuelTypes[3] = (TextView) getView().findViewById(R.id.textViewTableRow32);
		fuelTypes[4] = (TextView) getView().findViewById(R.id.textViewTableRow42);
		fuelTypes[5] = (TextView) getView().findViewById(R.id.textViewTableRow52);
		fuelTypes[6] = (TextView) getView().findViewById(R.id.textViewTableRow62);
		fuelTypes[7] = (TextView) getView().findViewById(R.id.textViewTableRow72);
		
		currentOdos[0] = (TextView) getView().findViewById(R.id.textViewTableRow03);
		currentOdos[1] = (TextView) getView().findViewById(R.id.textViewTableRow13);
		currentOdos[2] = (TextView) getView().findViewById(R.id.textViewTableRow23);
		currentOdos[3] = (TextView) getView().findViewById(R.id.textViewTableRow33);
		currentOdos[4] = (TextView) getView().findViewById(R.id.textViewTableRow43);
		currentOdos[5] = (TextView) getView().findViewById(R.id.textViewTableRow53);
		currentOdos[6] = (TextView) getView().findViewById(R.id.textViewTableRow63);
		currentOdos[7] = (TextView) getView().findViewById(R.id.textViewTableRow73);
		
		try{
			 FileInputStream fileIn = new FileInputStream("/containers/journalDataContainer.xml");
	         ObjectInputStream in = new ObjectInputStream(fileIn);
	         container = (JournalsDataContainer) in.readObject();
	         in.close();
	         fileIn.close();
		}
		catch(IOException i) {
	         container = new JournalsDataContainer();
	    }catch(ClassNotFoundException c){
	         System.out.println("Class not found");
	         c.printStackTrace();
	    }
        updateView();
	}
	
	private void updateView()
	{
		int iterator;
		for (int i = 0; i < 8; i++)
		{
			iterator = container.refuels.size() - 1 - i;
			if (iterator >= 0)
			{
				dates[i].setText(container.refuels.get(iterator).date.toString());
				fuelAmounts[i].setText(container.refuels.get(iterator).amount + " l");
				switch (container.refuels.get(iterator).fuelType)
				{
				case 0:
					fuelTypes[i].setText("PB95");
					break;
				case 1:
					fuelTypes[i].setText("PB98");
					break;
				case 2:
					fuelTypes[i].setText("ON");
					break;
				}
				currentOdos[i].setText(container.refuels.get(iterator).odo + " km");
			}
			else
			{
				dates[i].setText("-");
				fuelAmounts[i].setText("-");
				fuelTypes[i].setText("-");
				currentOdos[i].setText("-");
			}
		}
	}
	
	private void SaveContainer()
	{
		try
	      {
	         FileOutputStream fileOut =
	         new FileOutputStream("/containers/journalDataContainer.xml");
	         ObjectOutputStream out = new ObjectOutputStream(fileOut);
	         out.writeObject(container);
	         out.close();
	         fileOut.close();
	      }catch(IOException i)
	      {
	          i.printStackTrace();
	      }
	}
}
